package com.illtamer.infinite.sillage.controller;

import com.illtamer.infinite.sillage.service.BlogService;
import com.illtamer.infinite.sillage.service.TagService;
import com.illtamer.infinite.sillage.service.TypeService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;

@Controller
public class IndexController {

    @Resource(name = "blogService")
    private BlogService blogService;

    @Resource(name = "typeService")
    private TypeService typeService;

    @Resource(name = "tagService")
    private TagService tagService;

    @RequestMapping({"/index", "/"})
    public String index(@RequestParam(name = "index", required = false, defaultValue = "1") Integer index
            , Model model) {
        model.addAttribute("pages", blogService.listRecommendHomeBlog(index))
                .addAttribute("types", typeService.listThinTypes())
                .addAttribute("tags", tagService.listThinTags())
                .addAttribute("latestList", blogService.listLatestMinBlog());
        return "index";
    }

    @RequestMapping("/search")
    public String search(@RequestParam(name = "index", required = false, defaultValue = "1") Integer index,
                         @RequestParam("content") String content, Model model) {
        model.addAttribute("pages", blogService.searchHomeBlog(content, index))
                // 翻页时保存查询条件
                .addAttribute("content", content);
        return "search";
    }

}
